<?php
class Cl_Comment extends WdkClass 
{
	protected
		/******************************************************/
		//	원본 테이블 정보
		/******************************************************/
		$comment_seq			= 0					,	//	
		$post_seq				= 0					,	//	
		$user_seq				= 0					,	//	
		$comment_index			= 0					,	//	
		$comment_depth			= 0					,	//	
		$comment_score			= 0					,	//	
		$comment_content		= ''				,	//	
		$comment_secure_flag	= 'F'				,	//	
		$comment_time			= 0					,	//	
		$comment_ip				= ''				,	//	
		$comment_del_flag		= 'F'				,	//	
		/******************************************************/
		//	추가 정보
		/******************************************************/
		$user_login_id		= ''				,	//	
		$user_icon			= ''				,	//	
		$user_nick			= ''				,	//	
		$user_name			= ''				,	//	
		$user_nick_str		= ''				,	//	
		$comment_time_str	= ''				,	//	
		$comment_ip_str		= ''				;	//	
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
	}
	
	/**
	 *	
	 */
	public function load($comment_seq, $bbs=null)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($comment_seq));
		if($bbs !== null && is_object($bbs))
		{
			$this->bbs = $bbs;
		}
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		$this->comment_time_str	= parent::timeString($this->comment_time);
		$this->comment_ip_str	= parent::ipString($this->comment_ip_str);
		$this->user_nick_str	= $this->user_nick;
		if($this->user_icon)
		{
			$this->user_nick_str = "<img src='{$this->user_icon}' class='user_icon'/>";
		}
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->comment_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 * 
	 */
	public function isEditable()
	{
		$session = Wdk::helper('Session');
		if($this->bbs->isAdmin()
		|| ($session->isLogin() && $session['user_seq'] == $this->user_seq))
		{
			return true;
		}
		return false;
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert', $indexing=true)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		$session = Wdk::helper('Session');
		/*------------------------------------------------------------*/
		
		$this->user_seq				= $session['user_seq'];
		$this->comment_time			= time();
		$this->comment_del_flag		= 'F';
		
		$data = array(	'post_seq'				=>	$this->post_seq,
						'user_seq'				=>	$this->user_seq,
						'comment_index'			=>	$indexing ? $dbo->nextIndex($this->post_seq) : $this->comment_index,
						'comment_depth'			=>	$this->comment_depth,
						'comment_score'			=>	$this->comment_score,
						'comment_content'		=>	$this->comment_content,
						'comment_secure_flag'	=>	$this->comment_secure_flag,
						'comment_time'			=>	$this->comment_time,
						'comment_ip'			=>	isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',
						'comment_del_flag'		=>	$this->comment_del_flag
					);
		$this->comment_seq = $dbo->insert($data);
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		if(parent::isUpdatable())
		{
			$data = array(	'comment_score'			=>	$this->comment_score,
							'comment_content'		=>	$this->comment_content,
							'comment_secure_flag'	=>	$this->comment_secure_flag
						);
			$dbo->update($data, array("comment_seq = ".$this->comment_seq));
			/******************************************************/
			parent::setBackupData(array_keys($data));
			$this->backup($log);
			/******************************************************/
		}
	}
	
	/**
	 * 
	 */
	public function delete($log='delete')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->delete(array("comment_seq = ".$this->comment_seq));
		$this->backup($log);
		/******************************************************/
		parent::setBackupData();
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function reply($user_seq=0, $content='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$comment = Wdk::get('Comment');
		$comment['post_seq'] 		= $this->post_seq;
		$comment['user_seq']		= $user_seq;
		$comment['comment_index']	= $this->comment_index+1;
		$comment['comment_depth']	= $this->comment_depth + 1;
		$comment['comment_content']	= $content;
		$dbo->pushIndex($this->post_seq, $this->comment_index);
		$comment->insert("덧글등록", false);

	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->log($this->comment_seq, $log, $this->backup_data);
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}